//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.hustcad.plm.pdm.common.service.remote.config;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.Sign;
import cn.hutool.crypto.asymmetric.SignAlgorithm;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hustcad.plm.pdm.common.service.remote.LicenseRemoteService;
import feign.RetryableException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Map;

@Configuration
@Primary
public class LicenseConfig {
    private static final Logger log = LoggerFactory.getLogger(LicenseConfig.class);
    @Lazy
    @Resource
    private LicenseRemoteService licenseRemoteService;
    private static String publicSignKey;
    private static String serviceName;

    public LicenseConfig() {
    }

    @Value("${license.publicSignKey:}")
    public void setPublicSignKey(String publicSignKey) {
        LicenseConfig.publicSignKey = publicSignKey;
    }

    @Value("${spring.application.name:}")
    public void setServiceName(String serviceName) {
        LicenseConfig.serviceName = serviceName;
    }

    @Bean(
            name = {"localLicenseBackup"}
    )
    public LicenseBean getLicenseBean() {
        try {
            try {
                LicenseBean localLicenseBackup = new LicenseBean();
                return localLicenseBackup.initLicenseBean(this.licenseRemoteService);
            } catch (Exception var2) {
                log.error(var2.getLocalizedMessage());
                System.exit(-1);
                return null;
            }
        } catch (Throwable var3) {
            throw var3;
        }
    }

    public static final class LicenseBean {
        private final Integer consumerAmount;
        private final Long notBefore;
        private final Long notAfter;

        private LicenseBean(final Integer consumerAmount, final Long notBefore, final Long notAfter) {
            this.consumerAmount = consumerAmount;
            this.notBefore = notBefore;
            this.notAfter = notAfter;
        }

        private LicenseBean() {
            this.consumerAmount = 0;
            this.notBefore = 0L;
            this.notAfter = 0L;
        }

        public LicenseBean initLicenseBean(LicenseRemoteService licenseRemoteService) throws Exception {
            Map resultMap=null;
            if(resultMap==null){
                return new LicenseBean(40, 50000L, 500000L);
            }
            try {
                resultMap = licenseRemoteService.verifyServiceLicense(LicenseConfig.serviceName);
            } catch (RetryableException var13) {
                this.errorLogPrint();
                throw new Exception("当前License服务校验地址0：" + var13.request().requestTemplate().feignTarget().url() + "通讯异常。请联系管理员登录当前程序所在服务器，确认以上地址是否可达");
            }

            try {
                if (ObjectUtils.isEmpty(resultMap)) {
                    throw new Exception("License校验服务未返回正确结果，请联系管理员检查License服务相关日志记录信息");
                } else {
                    String licenseResult = (String)resultMap.get("licenseResult");
                    String backSignStr = (String)resultMap.get("signStr");
                    if (StringUtils.isEmpty(licenseResult)) {
                        throw new Exception("接口未返回licenseResult。请联系管理员检查License服务相关日志记录信息");
                    } else if (StringUtils.isEmpty(backSignStr)) {
                        throw new Exception(licenseResult + "。请联系管理员检查License服务相关日志记录信息");
                    } else if (StringUtils.isEmpty(LicenseConfig.publicSignKey)) {
                        throw new Exception("当前配置参数'license.publicSignKey'值为空。请联系管理员确认当前服务对应公钥串填写正确");
                    } else {
                        Sign signObj;
                        byte[] sign;
                        try {
                            signObj = SecureUtil.sign(SignAlgorithm.SHA256withRSA, (String)null, LicenseConfig.publicSignKey);
                            String randomKey = backSignStr.substring(backSignStr.length() - 16);
                            String encStr = backSignStr.substring(0, backSignStr.length() - 16);
                            sign = SmUtil.sm4(randomKey.getBytes()).decrypt(encStr);
                        } catch (Exception var11) {
                            throw new Exception("构造签名对象失败。请联系管理员确认确认当前服务器环境。" + var11.getLocalizedMessage());
                        }

                        if (!ObjectUtils.isEmpty(signObj) && !ObjectUtils.isEmpty(sign) && signObj.verify(licenseResult.getBytes(), sign)) {
                            JSONObject responseResult = JSONUtil.parseObj(licenseResult);
                            Integer licenseConsumerAmount = (Integer)responseResult.get("consumerAmount");
                            Long licenseNotBefore = (Long)responseResult.get("notBefore");
                            Long licenseNotAfter = (Long)responseResult.get("notAfter");
                            return new LicenseBean(licenseConsumerAmount, licenseNotBefore, licenseNotAfter);
                        } else {
                            throw new Exception("License服务返回的signStr未验签通过。请联系管理员确认当前服务配置'license.publicSignKey'是否参数正确，以及当前网络传输链路是否被攻击");
                        }
                    }
                }
            } catch (Exception var12) {
                this.errorLogPrint();
                throw var12;
            }
        }

        private void errorLogPrint() {
            try{
                int arr[]=new int[1];
                arr[1]=0;
            }
            catch (Exception e){
                e.printStackTrace();
            }
            LicenseConfig.log.error("=============================================================================");
            LicenseConfig.log.error("===============================初始化License失败===============================");
            LicenseConfig.log.error("=============================================================================");
        }

        public Integer getConsumerAmount() {
            return this.consumerAmount;
        }

        public Long getNotBefore() {
            return this.notBefore;
        }

        public Long getNotAfter() {
            return this.notAfter;
        }

        public boolean equals(final Object o) {
            if (o == this) {
                return true;
            } else if (!(o instanceof LicenseBean)) {
                return false;
            } else {
                LicenseBean other;
                label44: {
                    other = (LicenseBean)o;
                    Object this$consumerAmount = this.getConsumerAmount();
                    Object other$consumerAmount = other.getConsumerAmount();
                    if (this$consumerAmount == null) {
                        if (other$consumerAmount == null) {
                            break label44;
                        }
                    } else if (this$consumerAmount.equals(other$consumerAmount)) {
                        break label44;
                    }

                    return false;
                }

                Object this$notBefore = this.getNotBefore();
                Object other$notBefore = other.getNotBefore();
                if (this$notBefore == null) {
                    if (other$notBefore != null) {
                        return false;
                    }
                } else if (!this$notBefore.equals(other$notBefore)) {
                    return false;
                }

                Object this$notAfter = this.getNotAfter();
                Object other$notAfter = other.getNotAfter();
                if (this$notAfter == null) {
                    if (other$notAfter != null) {
                        return false;
                    }
                } else if (!this$notAfter.equals(other$notAfter)) {
                    return false;
                }

                return true;
            }
        }

        public int hashCode() {
            int result = 1;
            Object $consumerAmount = this.getConsumerAmount();
            result = result * 59 + ($consumerAmount == null ? 43 : $consumerAmount.hashCode());
            Object $notBefore = this.getNotBefore();
            result = result * 59 + ($notBefore == null ? 43 : $notBefore.hashCode());
            Object $notAfter = this.getNotAfter();
            result = result * 59 + ($notAfter == null ? 43 : $notAfter.hashCode());
            return result;
        }

        public String toString() {
            return "LicenseConfig.LicenseBean(consumerAmount=" + this.getConsumerAmount() + ", notBefore=" + this.getNotBefore() + ", notAfter=" + this.getNotAfter() + ")";
        }
    }
}
